{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e62bfc71-e230-4c27-bfc2-18636cdeeb63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第 1个URL:http://www.abc.com/EXCELYZ1.jpg\n",
      "第 2个URL:http://www.xyz.com/EXCELYZ2.jpg\n",
      "第 3个URL:http://www.abc.com/EXCELYZ3.jpg\n",
      "第 4个URL:http://www.xyz.com/EXCELYZ4.jpg\n",
      "第 5个URL:http://www.abc.com/t/EXCELYZ5.jpg\n"
     ]
    }
   ],
   "source": [
    "#读取HTML文件内容\n",
    "def getHTMLlines(htmlpath):\n",
    "    f = open(htmlpath,\"r\",encoding = 'utf-8')\n",
    "    ls = f.readlines()  \n",
    "    f.close()\n",
    "    return  ls\n",
    "#用于解析文件并提取图像的URL\n",
    "def extractImageUrls(htmllist):\n",
    "    urls = []\n",
    "    for line in htmllist:\n",
    "        if 'img' in line:\n",
    "            url = line.split('src=')[-1].split('\"')[1]\n",
    "            if 'http' in url:\n",
    "                urls.append(url)\n",
    "    return urls\n",
    "#将获取的链接输出到屏幕上\n",
    "def showResults(urls):\n",
    "    count = 1\n",
    "    for url in urls :\n",
    "        print(\"第{:2}个URL:{}\".format(count,url))\n",
    "        count += 1\n",
    "# 主程序：1 读取文件；2 解析并提取其中的图片链接；3 输出提取结果到屏幕\n",
    "def main():\n",
    "    inputfile = \"chinadaily.html\"\n",
    "    htmllines = getHTMLlines(inputfile)\n",
    "    imageUrls = extractImageUrls(htmllines)\n",
    "    showResults(imageUrls)\n",
    "if __name__ == '__main__': main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b21910ae-327e-43bf-8d0f-eb9db97b3d73",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "689f48e2-2301-4550-95ca-ef25c3378b39",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
